package com.ktgj.pay.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ktgj.pay.domain.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SendInvoiceInterfaceUtil {
	
	/**
	 * 发送请求函数
	 * @param result
	 * @return
	 * @throws IOException
	 */
	public static String sendInvoice(String result) throws IOException{
		//第一步：创建服务地址  
        URL url = new URL("http://172.16.2.39:8089/eisp-box/ws/invWS?wsdl");  
        //第二步：打开一个通向服务地址的连接  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        //第三步：设置参数  
        //3.1发送方式设置：POST必须大写  
        connection.setRequestMethod("POST");  
        //3.2设置数据格式：content-type  
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");  
        //3.3设置输入输出，因为默认新创建的connection没有读写权限，  
        connection.setDoInput(true);  
        connection.setDoOutput(true);  
  
        //第四步：组织SOAP数据，发送请求  
        String soapXML = getSendModelXml(result);  
        //将信息以流的方式发送出去
        OutputStream os = connection.getOutputStream();  
        os.write(soapXML.getBytes());  
        //第五步：接收服务端响应，打印  
        int responseCode = connection.getResponseCode(); 
        String response = "";  
        if(200 == responseCode){//表示服务端响应成功 
        	//获取当前连接请求返回的数据流
            InputStream is = connection.getInputStream();  
            InputStreamReader isr = new InputStreamReader(is,"UTF-8");  
            BufferedReader br = new BufferedReader(isr);  
              
            StringBuilder sb = new StringBuilder();  
            String temp = null;  
            while(null != (temp = br.readLine())){  
                sb.append(temp);  
            }  
            
            /**
             * 打印结果
             */
            response = getResponseXml(sb.toString());
//            System.out.println(response);
            
            is.close();  
            isr.close();  
            br.close();  
        }else{
        	response = "false";
        }
        os.close(); 
        return response;
	}
	
	/**
	 * 封账请求参数
	 * @param result
	 * @return
	 */
	public static String getSendModelXml(String result){
		String soapXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.externalinterface.ejinshui.com/\">"  
    	        +"<soapenv:Header/> " 
    			+"<soapenv:Body> " 
    	        +"<web:invWS> " 
    			+"<!--Optional:--> "
    			+ " <arg0>"
    	        + result
    	        +"</arg0>"  
    	        +"</web:invWS>"  
    	        +"</soapenv:Body>"
    	        + "</soapenv:Envelope>";  
        return soapXML;
	}
	
	/**
	 * 解析返回参数
	 * @param result
	 * @return
	 */
	public static String getResponseXml(String result){
		String resopnse = "";
		resopnse = result.replace("&lt;", "<").replace("&gt;", ">");
		resopnse = resopnse.replace("&quot;", "\"");
		resopnse = resopnse.trim();
		String[] arr = resopnse.split("<return>");
		String[] arr2 = arr[1].split("</return>");
		resopnse = arr2[0];
		
		return resopnse;
	}
	
	/**
	 * 硬解析返回模板
	 * @param xml
	 * @return
	 */
	public static MoResponse getResponseXmlInfo(String xml)
	  {
		MoResponse response = new MoResponse();
		Document doc2 = Jsoup.parse(xml, "utf-8");
		String version = doc2.select("baseInfo").get(0).select("version").isEmpty()?"":doc2.select("baseInfo").get(0).select("version").get(0).text();
		String sellerTaxNo = doc2.select("baseInfo").get(0).select("sellerTaxNo").isEmpty()?"":doc2.select("baseInfo").get(0).select("sellerTaxNo").get(0).text();
		String interfaceCode = doc2.select("baseInfo").get(0).select("interfaceCode").isEmpty()?"":doc2.select("baseInfo").get(0).select("interfaceCode").get(0).text();
		String returnCode = doc2.select("returnInfo").get(0).select("returnCode").isEmpty()?"":doc2.select("returnInfo").get(0).select("returnCode").get(0).text();
		String returnMessage = doc2.select("returnInfo").get(0).select("returnMessage").isEmpty()?"":doc2.select("returnInfo").get(0).select("returnMessage").get(0).text();
		String zipCode = doc2.select("exData").get(0).select("zipCode").isEmpty()?"":doc2.select("exData").get(0).select("zipCode").get(0).text();
		String encryptCode = doc2.select("exData").get(0).select("encryptCode").isEmpty()?"":doc2.select("exData").get(0).select("encryptCode").get(0).text();
		String content = doc2.select("exData").get(0).select("content").isEmpty()?"":doc2.select("exData").get(0).select("content").get(0).text();
//	      Elements root = doc2.getAllElements();
//	      String version = ((Element)root.select("baseInfo").get(0).select("version").get(0)).text();
//	      String sellerTaxNo = ((Element)root.select("baseInfo").get(0).select("sellerTaxNo").get(0)).text();
//	      String interfaceCode = ((Element)root.select("baseInfo").get(0).select("interfaceCode").get(0)).text();
//	      String returnCode = ((Element)root.select("returnInfo").get(0).select("returnCode").get(0)).text();
//	      String returnMessage = ((Element)root.select("returnInfo").get(0).select("returnMessage").get(0)).text();
//	      String zipCode = ((Element)root.select("exData").get(0).select("zipCode").get(0)).text();
//	      String encryptCode = ((Element)root.select("exData").get(0).select("encryptCode").get(0)).text();
//	      String content = ((Element)root.select("exData").get(0).select("content").get(0)).text();
	      
	      //数据存储-基础请求信息
	      MoBaseInfo baseInfo = new MoBaseInfo();
	      baseInfo.setVersion(version);
	      baseInfo.setInterfaceCode(interfaceCode);
	      baseInfo.setSellerTaxNo(sellerTaxNo);
	      response.setBaseInfo(baseInfo);
	      
	      //数据存储-接口返回提示
	      MoReturnInfo returnInfo = new MoReturnInfo();
	      returnInfo.setReturnCode(returnCode);
	      returnInfo.setReturnMessage(returnMessage);
	      response.setReturnInfo(returnInfo);
	      
	      //数据存储-接口内层返回
	      MoExData exData = new MoExData();
	      exData.setZipCode(zipCode);
	      exData.setEncryptCode(encryptCode);
	      exData.setContent(content);
	      response.setExData(exData);

	    return response;
	  }
	
	//------------------------------------------------------------------------------发票开具---分割线---------------------------------------------------------------------------------
	
	/**
	 * 内层返回-发票开具接口-硬解析
	 * @param content
	 * @return
	 */
	public static MoFPKJResponse getFPKJResult(String content){
		MoFPKJResponse fpkjResponse = new MoFPKJResponse();
		/*
		 * base64解码
		 */
		String xml = BaseUtil.Decide(content);
		/*
		 * 硬解析封装数据
		 */
		Document doc2 = Jsoup.parse(xml, "utf-8");
		String returnCode = doc2.select("responseFPKJ").get(0).select("returnCode").isEmpty()?"":doc2.select("responseFPKJ").get(0).select("returnCode").get(0).text();
		String returnMessage = doc2.select("responseFPKJ").get(0).select("returnMessage").isEmpty()?"":doc2.select("responseFPKJ").get(0).select("returnMessage").get(0).text();
		fpkjResponse.setReturnCode(returnCode);
		fpkjResponse.setReturnMessage(returnMessage);
		if(Objects.equals(returnCode, "1000")){
			String swno = doc2.select("responseFPKJ").get(0).select("swno").isEmpty()?"":doc2.select("responseFPKJ").get(0).select("swno").get(0).text();
			String sellerTaxNo = doc2.select("responseFPKJ").get(0).select("sellerTaxNo").isEmpty()?"":doc2.select("responseFPKJ").get(0).select("sellerTaxNo").get(0).text();
	//	    Elements root = doc2.getAllElements();
	//	    String returnCode = ((Element)root.select("responseFPKJ").get(0).select("returnCode").get(0)).text();
	//	    String returnMessage = ((Element)root.select("responseFPKJ").get(0).select("returnMessage").get(0)).text();
	//	    String swno = ((Element)root.select("responseFPKJ").get(0).select("swno").get(0)).text();
	//	    String sellerTaxNo = ((Element)root.select("responseFPKJ").get(0).select("sellerTaxNo").get(0)).text();
			fpkjResponse.setSwno(swno);
			fpkjResponse.setSellerTaxNo(sellerTaxNo);
		}
	    
		return fpkjResponse;
	}
	/**
	 * 解析封装发票开具实体
	 * @param Json
	 * @return
	 */
	public static MoFPKJRequest getFPKJRequest(String Json){
		MoFPKJRequest request = new MoFPKJRequest();
		JSONObject JSR = JSONObject.parseObject(Json);
		/**
		 * 必填
		 */
		request.setSwno(JSR.getString("swno"));
		request.setSellerTaxNo(JSR.getString("sellerTaxNo"));
		request.setCustName(JSR.getString("custName"));
		request.setInvType(JSR.getString("invType"));
		request.setOrderType(JSR.getString("orderType"));
		request.setOilIdentification(JSR.getString("oilIdentification"));
		request.setKpType(JSR.getString("kpType"));
		
		/**
		 * 非必填
		 */
		if(RegexUtil.isNotNull(JSR.getString("custTaxNo"))){
			request.setCustTaxNo(JSR.getString("custTaxNo"));
		}
		if(RegexUtil.isNotNull(JSR.getString("custAddrPhone"))){
			request.setCustAddrPhone(JSR.getString("custAddrPhone"));
		}
		if(RegexUtil.isNotNull(JSR.getString("custBankAccount"))){
			request.setCustBankAccount(JSR.getString("custBankAccount"));
		}
		if(RegexUtil.isNotNull(JSR.getString("custTelephone"))){
			request.setCustTelephone(JSR.getString("custTelephone"));
		}
		if(RegexUtil.isNotNull(JSR.getString("custEmail"))){
			request.setCustEmail(JSR.getString("custEmail"));
		}
		if(RegexUtil.isNotNull(JSR.getString("invMemo"))){
			request.setInvMemo(JSR.getString("invMemo"));
		}
		if(RegexUtil.isNotNull(JSR.getString("orderDate"))){
			request.setOrderDate(JSR.getString("orderDate"));
		}
		if(RegexUtil.isNotNull(JSR.getString("drawer"))){
			request.setDrawer(JSR.getString("drawer"));
		}
		if(RegexUtil.isNotNull(JSR.getString("reviewer"))){
			request.setReviewer(JSR.getString("reviewer"));
		}
		if(RegexUtil.isNotNull(JSR.getString("payee"))){
			request.setPayee(JSR.getString("payee"));
		}
		if(Objects.equals(request.getOrderType(),"2")){ //红票必填
			request.setOrigInvcode(JSR.getString("origInvcode"));
			request.setOrigInvno(JSR.getString("origInvno"));
			request.setRedCode(JSR.getString("redCode"));
		}
		if(RegexUtil.isNotNull(JSR.getString("sellerAddrPhone"))){
			request.setSellerAddrPhone(JSR.getString("sellerAddrPhone"));
		}
		if(RegexUtil.isNotNull(JSR.getString("sellerBankAccount"))){
			request.setSellerBankAccount(JSR.getString("sellerBankAccount"));
		}
		if(Objects.equals(request.getKpType(),"0") && !Objects.equals(request.getInvType(),"51")){
			request.setMachineCode(JSR.getString("machineCode"));
		}else{
			if(RegexUtil.isNotNull(JSR.getString("machineCode"))){
				request.setMachineCode(JSR.getString("machineCode"));
			}
		}
		/**
		 * 商品集合
		 */
		if(RegexUtil.isNotNull(JSR.getJSONArray("items"))){
			request.setItems(getFPKJItems(JSR.getJSONArray("items")));
		}
		
		return request;
	}
	/**
	 * 解析封装发票开具实体-开票商品
	 * @param array
	 * @return
	 */
	public static List<MoFPKJItem> getFPKJItems(JSONArray array){
		List<MoFPKJItem> items = new ArrayList<MoFPKJItem>();
		for(int i = 0; i < array.size(); i++){
			JSONObject object = array.getJSONObject(i);
			MoFPKJItem item = new MoFPKJItem();
			/**
			 * 必填
			 */
			item.setLineType(object.getString("lineType"));
			item.setLineCode(object.getString("lineCode"));
			item.setGoodsName(object.getString("goodsName"));
			item.setTaxRate(object.getString("taxRate"));
			item.setTaxCode(object.getString("taxCode"));
			item.setTaxPre(object.getString("taxPre"));
			
			
			/**
			 * 非必填
			 */
			if(RegexUtil.isNotNull(object.getString("model"))){
				item.setModel(object.getString("model"));
			}
			if(RegexUtil.isNotNull(object.getString("unit"))){
				item.setUnit(object.getString("unit"));
			}
			if(RegexUtil.isNotNull(object.getString("qty"))){
				item.setQty(object.getString("qty"));
			}
			if(RegexUtil.isNotNull(object.getString("price"))){
				item.setPrice(object.getString("price"));
			}
			if(RegexUtil.isNotNull(object.getString("taxPrice"))){
				item.setTaxPrice(object.getString("taxPrice"));
			}
			if(RegexUtil.isNotNull(object.getString("amount"))){
				item.setAmount(object.getString("amount"));
			}
			if(RegexUtil.isNotNull(object.getString("taxAmount"))){
				item.setTaxAmount(object.getString("taxAmount"));
			}
			if(RegexUtil.isNotNull(object.getString("tax"))){
				item.setTax(object.getString("tax"));
			}
			if(Objects.equals(item.getTaxPre(),"1")){
				item.setTaxPrecon(object.getString("taxPrecon"));
			}else{
				if(RegexUtil.isNotNull(object.getString("taxPrecon"))){
					item.setTaxPrecon(object.getString("taxPrecon"));
				}
			}
			if(RegexUtil.isNotNull(object.getString("zeroTax"))){
				item.setZeroTax(object.getString("zeroTax"));
			}
			if(RegexUtil.isNotNull(object.getString("gropGoodsNo"))){
				item.setGropGoodsNo(object.getString("gropGoodsNo"));
			}
			if(RegexUtil.isNotNull(object.getString("taxDeduction"))){
				item.setTaxDeduction(object.getString("taxDeduction"));
			}
			if(RegexUtil.isNotNull(object.getString("taxVersion"))){
				item.setTaxVersion(object.getString("taxVersion"));
			}
			items.add(item);
		}
		return items;
	}
	
	/**
	 * 封装开具发票参数为xml
	 * @param request
	 * @return
	 */
	public static String getFPKJRequestXml(MoFPKJRequest request){
		String context = "";
		context = context + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+"\n";
		context = context + "<requestFPKJ>"+"\n";
		context = context + "<swno>"+request.getSwno()+"</swno>"+"\n";
		context = context + "<sellerTaxNo>"+request.getSellerTaxNo()+"</sellerTaxNo>"+"\n";
		context = context + "<custName>"+request.getCustName()+"</custName>"+"\n";
		if(RegexUtil.isNotNull(request.getCustTaxNo())){
			context = context + "<custTaxNo>"+request.getCustTaxNo()+"</custTaxNo>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getCustAddrPhone())){
			context = context + "<custAddrPhone>"+request.getCustAddrPhone()+"</custAddrPhone>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getCustBankAccount())){
			context = context + "<custBankAccount>"+request.getCustBankAccount()+"</custBankAccount>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getCustTelephone())){
			context = context + "<custTelephone>"+request.getCustTelephone()+"</custTelephone>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getCustEmail())){
			context = context + "<custEmail>"+request.getCustEmail()+"</custEmail>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getInvMemo())){
			context = context + "<invMemo>"+request.getInvMemo()+"</invMemo>"+"\n";
		}
		context = context + "<invType>"+request.getInvType()+"</invType>";
		if(RegexUtil.isNotNull(request.getOrderDate())){
			context = context + "<orderDate>"+request.getOrderDate()+"</orderDate>"+"\n";
		}
		context = context + "<orderType>"+request.getOrderType()+"</orderType>"+"\n";
		if(RegexUtil.isNotNull(request.getDrawer())){
			context = context + "<drawer>"+request.getDrawer()+"</drawer>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getReviewer())){
			context = context + "<reviewer>"+request.getReviewer()+"</reviewer>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getPayee())){
			context = context + "<payee>"+request.getPayee()+"</payee>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getOrigInvcode())){
			context = context + "<origInvcode>"+request.getOrigInvcode()+"</origInvcode>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getOrigInvno())){
			context = context + "<origInvno>"+request.getOrigInvno()+"</origInvno>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getRedCode())){
			context = context + "<redCode>"+request.getRedCode()+"</redCode>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getSellerAddrPhone())){
			context = context + "<sellerAddrPhone>"+request.getSellerAddrPhone()+"</sellerAddrPhone>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getSellerBankAccount())){
			context = context + "<sellerBankAccount>"+request.getSellerBankAccount()+"</sellerBankAccount>"+"\n";
		}
		if(RegexUtil.isNotNull(request.getMachineCode())){
			context = context + "<machineCode>"+request.getMachineCode()+"</machineCode>"+"\n";
		}
		context = context + "<oilIdentification>"+request.getOilIdentification()+"</oilIdentification>"+"\n";
		context = context + "<kpType>"+request.getKpType()+"</kpType>"+"\n";
		
		context = context + "<items>"+getFPKJItemsString(request.getItems())+"</items>"+"\n";
		context = context + "</requestFPKJ>"+"\n";
		return context;
	}
	/**
	 * 封装开具发票参数为xml-开票商品
	 * @param items
	 * @return
	 */
	public static String getFPKJItemsString(List<MoFPKJItem> items){
		String context = "";
		for(int a = 0; a < items.size(); a++){
			MoFPKJItem item = items.get(a);
			context = context + "<item>";
			context = context + "<goodsName>"+item.getGoodsName()+"</goodsName>";
			context = context + "<lineCode>"+item.getLineCode()+"</lineCode>";
			context = context + "<lineType>"+item.getLineType()+"</lineType>";
			if(RegexUtil.isNotNull(item.getModel())){
				context = context + "<model>"+item.getModel()+"</model>";
			}
			if(RegexUtil.isNotNull(item.getUnit())){
				context = context + "<unit>"+item.getUnit()+"</unit>";
			}
			if(RegexUtil.isNotNull(item.getQty())){
				context = context + "<qty>"+item.getQty()+"</qty>";
			}else{
				context = context + "<qty>"+1+"</qty>";
			}
			if(RegexUtil.isNotNull(item.getPrice())){
				context = context + "<price>"+item.getPrice()+"</price>";
			}
			if(RegexUtil.isNotNull(item.getTaxPrice())){
				context = context + "<taxPrice>"+item.getTaxPrice()+"</taxPrice>";
			}
			if(RegexUtil.isNotNull(item.getAmount())){
				context = context + "<amount>"+item.getAmount()+"</amount>";
			}
			if(RegexUtil.isNotNull(item.getTaxAmount())){
				context = context + "<taxAmount>"+item.getTaxAmount()+"</taxAmount>";
			}
			if(RegexUtil.isNotNull(item.getTax())){
				context = context + "<tax>"+item.getTax()+"</tax>";
			}
			context = context + "<taxRate>"+item.getTaxRate()+"</taxRate>";
			context = context + "<taxCode>"+item.getTaxCode()+"</taxCode>";
			context = context + "<taxPre>"+item.getTaxPre()+"</taxPre>";
			if(Objects.equals(item.getTaxPre(),"1")){
				context = context + "<taxPrecon>"+item.getTaxPrecon()+"</taxPrecon>";
			}else{
				if(RegexUtil.isNotNull(item.getTaxPrecon())){
					context = context + "<taxPrecon>"+item.getTaxPrecon()+"</taxPrecon>";
				}
			}
			if(RegexUtil.isNotNull(item.getZeroTax())){
				context = context + "<zeroTax>"+item.getZeroTax()+"</zeroTax>";
			}
			if(RegexUtil.isNotNull(item.getGropGoodsNo())){
				context = context + "<gropGoodsNo>"+item.getGropGoodsNo()+"</gropGoodsNo>";
			}
			if(RegexUtil.isNotNull(item.getTaxDeduction())){
				context = context + "<taxDeduction>"+item.getTaxDeduction()+"</taxDeduction>";
			}
			if(RegexUtil.isNotNull(item.getTaxVersion())){
				context = context + "<taxVersion>"+item.getTaxVersion()+"</taxVersion>";
			}
			context = context + "</item>";
		}
		return context;
	}
	
	//------------------------------------------------------------------------------发票下载---分割线---------------------------------------------------------------------------------
	
	/**
	 * 解析封装发票下载请求实体
	 * @param Json
	 * @return
	 */
	public static MoFPXZRequest getFPXZRequest(String Json){
		MoFPXZRequest request = new MoFPXZRequest();
		JSONObject JSR = JSONObject.parseObject(Json);
		request.setSellerTaxNo(JSR.getString("sellerTaxNo"));
		request.setSwno(JSR.getString("swno"));
		return request;
	}
	/**
	 * 封装发票下载请求XML
	 * @param request
	 * @return
	 */
	public static String getFPXZRequestXml(MoFPXZRequest request){
		String context = "";
		context = context + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		context = context + "<requestFPXZ>";
		context = context + "<swno>"+request.getSwno()+"</swno>";
		context = context + "<sellerTaxNo>"+request.getSellerTaxNo()+"</sellerTaxNo>";;
		context = context + "</requestFPXZ>";
		return context;
	}
	
	public static MoFPXZResponse getFPXZResult(String content,String path){
		MoFPXZResponse response = new MoFPXZResponse();
		/*
		 * base64解码
		 */
		String xml = BaseUtil.Decide(content);
		/*
		 * 硬解析封装数据
		 */
		Document doc2 = Jsoup.parse(xml, "utf-8");
		String returnCode = doc2.select("responseFPXZ").get(0).select("returnCode").isEmpty()?"":doc2.select("responseFPXZ").get(0).select("returnCode").get(0).text();
		String returnMessage = doc2.select("responseFPXZ").get(0).select("returnMessage").isEmpty()?"":doc2.select("responseFPXZ").get(0).select("returnMessage").get(0).text();
		response.setReturnCode(returnCode);
		response.setReturnMessage(returnMessage);
		if(Objects.equals(returnCode, "2000")){
			String swno = doc2.select("responseFPXZ").get(0).select("swno").isEmpty()?"":doc2.select("responseFPXZ").get(0).select("swno").get(0).text();
			String sellerTaxNo = doc2.select("responseFPXZ").get(0).select("sellerTaxNo").isEmpty()?"":doc2.select("responseFPXZ").get(0).select("sellerTaxNo").get(0).text();
			List<MoFPXZInvoiceInfo> infos = new ArrayList<MoFPXZInvoiceInfo>();
			Elements elements = doc2.select("responseFPXZ").get(0).select("invoiceInfo");
			for(int x = 0; x < elements.size(); x++){
				MoFPXZInvoiceInfo info = new MoFPXZInvoiceInfo();
				Element element = elements.get(x);
				String invNo = element.select("invNo").isEmpty()?"":element.select("invNo").get(0).text();
				String invCode = element.select("invCode").isEmpty()?"":element.select("invCode").get(0).text();
				String invDate = element.select("invDate").isEmpty()?"":element.select("invDate").get(0).text();
				String totalTax = element.select("totalTax").isEmpty()?"":element.select("totalTax").get(0).text();
				String pdfContent = element.select("pdfContent").isEmpty()?"":element.select("pdfContent").get(0).text();
				String totalAmount = element.select("totalAmount").isEmpty()?"":element.select("totalAmount").get(0).text();
				/*
				 * 日期获取
				 */
				String dateSign = BaseUtil.getDateString("yyyyMMdd");
				/*
				 * 路径指向
				 * 电子发票保存的PDF文件名规则：YYYYMMDD_发票代码_发票号码.pdf
				 * 20191011_011001900411_14825673.pdf
				 */
//				path =  path+"/"+dateSign+"_"+invCode+"_"+invNo+".pdf"; 作废
//				path =  path+"\\"+dateSign+"_"+invCode+"_"+invNo+".pdf";
				path = path + "/" + swno + ".pdf";
				/*
				 * 转存pdf至服务器本地
				 */
				GetPDFFileUtil.BasetoPdffile(pdfContent, path);
				info.setInvNo(invNo);
				info.setInvCode(invCode);
				info.setInvDate(invDate);
				info.setTotalTax(totalTax);
				info.setTotalAmount(totalAmount);
				info.setPdfContent(path);
				infos.add(info);
			}
			response.setSwno(swno);
			response.setInfos(infos);
			response.setSellerTaxNo(sellerTaxNo);
		}
		return response;
	}
	
	
	//------------------------------------------------------------------------------发票冲红---分割线---------------------------------------------------------------------------------
	/**
	 * 发票冲红-请求实体Json解析，返回请求对象
	 * @param Json
	 * @return
	 */
	public static MoFPCHRequest getFPCHRequest(String Json){
		MoFPCHRequest request = new MoFPCHRequest();
		JSONObject JSR = JSONObject.parseObject(Json);
		request.setSwno(JSR.getString("swno"));
		request.setSellerTaxNo(JSR.getString("sellerTaxNo"));
		if(RegexUtil.isNotNull(JSR.getString("machineCode"))){
			request.setMachineCode(JSR.getString("machineCode"));
		}
		request.setKpType(JSR.getString("kpType"));
		request.setOrigInvcode(JSR.getString("origInvcode"));
		request.setOrigInvno(JSR.getString("origInvno"));
		return request;
	}
	/**
	 * 发票冲红-请求Xml封装
	 * @param request
	 * @return
	 */
	public static String getFPCHRequestXml(MoFPCHRequest request){
		String context = "";
		context = context + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		context = context + "<requestFPCH>";
		context = context + "<swno>"+request.getSwno()+"</swno>";
		context = context + "<sellerTaxNo>"+request.getSellerTaxNo()+"</sellerTaxNo>";
		if(RegexUtil.isNotNull(request.getMachineCode())){
			context = context + "<machineCode>"+request.getMachineCode()+"</machineCode>";
		}
		context = context + "<kpType>"+request.getKpType()+"</kpType>";
		context = context + "<origInvcode>"+request.getOrigInvcode()+"</origInvcode>";
		context = context + "<origInvno>"+request.getOrigInvno()+"</origInvno>";
		context = context + "</requestFPCH>";
		return context;
	}
	
	public static MoFPCHResponse getFPCHResult(String context){
		MoFPCHResponse response = new MoFPCHResponse();
		/*
		 * base64解码
		 */
		String xml = BaseUtil.Decide(context);
		/*
		 * 硬解析封装数据
		 */
		Document doc2 = Jsoup.parse(xml, "utf-8");
		String returnCode = doc2.select("responseFPCH").get(0).select("returnCode").isEmpty()?"":doc2.select("responseFPCH").get(0).select("returnCode").get(0).text();
		String returnMessage = doc2.select("responseFPCH").get(0).select("returnMessage").isEmpty()?"":doc2.select("responseFPCH").get(0).select("returnMessage").get(0).text();
		response.setReturnCode(returnCode);
		response.setReturnMessage(returnMessage);
		if(Objects.equals(returnCode, "3000")){
			String swno = doc2.select("responseFPCH").get(0).select("swno").isEmpty()?"":doc2.select("responseFPCH").get(0).select("swno").get(0).text();
			String sellerTaxNo = doc2.select("responseFPCH").get(0).select("sellerTaxNo").isEmpty()?"":doc2.select("responseFPCH").get(0).select("sellerTaxNo").get(0).text();
			response.setSwno(swno);
			response.setSellerTaxNo(sellerTaxNo);
		}
		return response;
	}
	
	//------------------------------------------------------------------------------发票余量查询---分割线---------------------------------------------------------------------------------
	/**
	 * 解析Json，封装发票余量查询  请求实体
	 * @param Json
	 * @return
	 */
	public static MoFPYLCXRequest getFpylcxRequest(String Json){
		MoFPYLCXRequest request = new MoFPYLCXRequest();
		JSONObject JSR = JSONObject.parseObject(Json);
		request.setSellerTaxNo(JSR.getString("sellerTaxNo"));
		if(RegexUtil.isNotNull(JSR.getString("machineCode"))){
			request.setMachineCode(JSR.getString("machineCode"));
		}
		if(RegexUtil.isNotNull(JSR.getString("invType"))){
			request.setInvType(JSR.getString("invType"));
		}
		return request;
	}
	
	/**
	 * 封装发票余量查询，请求接口xml
	 * @param request
	 * @return
	 */
	public static String getFpylcxRequestXml(MoFPYLCXRequest request){
		String context = "";
		context = context + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		context = context + "<requstFPYLCX>";
		context = context + "<sellerTaxNo>"+request.getSellerTaxNo()+"</sellerTaxNo>";
		if(RegexUtil.isNotNull(request.getMachineCode())){
			context = context + "<machineCode>"+request.getMachineCode()+"</machineCode>";
		}
		if(RegexUtil.isNotNull(request.getInvType())){
			context = context + "<invType>"+request.getInvType()+"</invType>";
		}
		context = context + "</requstFPYLCX>";
		return context;
	}
	
	/**
	 * 解析封装-发票余量查询 返回实体
	 * @param context
	 * @return
	 */
	public static MoFPYLCXResponse getFPYLCXResult(String context){
		MoFPYLCXResponse response = new MoFPYLCXResponse();
		/*
		 * base64解码
		 */
		String xml = BaseUtil.Decide(context);
		/*
		 * 硬解析封装数据
		 */
		Document doc2 = Jsoup.parse(xml, "utf-8");
		String returnCode = doc2.select("responseFPYLCX").get(0).select("returnCode").isEmpty()?"":doc2.select("responseFPYLCX").get(0).select("returnCode").get(0).text();
		String returnMessage = doc2.select("responseFPYLCX").get(0).select("returnMessage").isEmpty()?"":doc2.select("responseFPYLCX").get(0).select("returnMessage").get(0).text();
		response.setReturnCode(returnCode);
		response.setReturnMessage(returnMessage);
		if(Objects.equals(returnCode, "4000")){
			String sellerTaxNo = doc2.select("responseFPYLCX").get(0).select("sellerTaxNo").isEmpty()?"":doc2.select("responseFPYLCX").get(0).select("sellerTaxNo").get(0).text();
			Elements machinElements = doc2.select("machine");
			List<MoFPYLCXMachine> machines = new ArrayList<MoFPYLCXMachine>();
			for(int a = 0; a < machinElements.size(); a++){
				MoFPYLCXMachine machine = new MoFPYLCXMachine();
				Element machElement = machinElements.get(a);
				String machineCode = machElement.select("machineCode").get(0).text();
				List<MoFPYLCXInvSurplus> invSurpluss = new ArrayList<MoFPYLCXInvSurplus>();
				Elements invElements = machElement.select("invSurplus");
				for(int b = 0; b < invElements.size(); b++){
					MoFPYLCXInvSurplus invSurplus = new MoFPYLCXInvSurplus();
					Element invElement = invElements.get(b);
					String invType = invElement.select("invType").get(0).text();
					String invQty = invElement.select("invQty").get(0).text();
					invSurplus.setInvQty(invQty);
					invSurplus.setInvType(invType);
					invSurpluss.add(invSurplus);
				}
				machine.setMachineCode(machineCode);
				machine.setInvSurpluss(invSurpluss);
				machines.add(machine);
			}
			response.setSellerTaxNo(sellerTaxNo);
			response.setMachines(machines);
		}
		
		return response;
	}
	
	//------------------------------------------------------------------------------订单删除---分割线---------------------------------------------------------------------------------
	/**
	 * 解析封装，订单删除请求实体
	 * @param Json
	 * @return
	 */
	public static MoDDSCRequest getDDSCRequest(String Json){
		MoDDSCRequest request = new MoDDSCRequest();
		JSONObject JSR = JSONObject.parseObject(Json);
		request.setSellerTaxNo(JSR.getString("sellerTaxNo"));
		request.setSwno(JSR.getString("swno"));
		return request;
	}
	/**
	 * 封装订单删除，请求xml
	 * @param request
	 * @return
	 */
	public static String getDDSCRequestXml(MoDDSCRequest request){
		String context = "";
		context = context + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		context = context + "<requstDDSC>";
		context = context + "<sellerTaxNo>"+request.getSellerTaxNo()+"</sellerTaxNo>";
		context = context + "<swno>"+request.getSwno()+"</swno>";
		context = context + "</requstDDSC>";
		return context;
	}
	/**
	 * 封装订单删除，返回实体
	 * @param context
	 * @return
	 */
	public static MoDDSCResponse getDDSCResult(String context){
		MoDDSCResponse response = new MoDDSCResponse();
		/*
		 * base64解码
		 */
		String xml = BaseUtil.Decide(context);
		/*
		 * 硬解析封装数据
		 */
		Document doc2 = Jsoup.parse(xml, "utf-8");
		String returnCode = doc2.select("responsDDSC").get(0).select("returnCode").isEmpty()?"":doc2.select("responsDDSC").get(0).select("returnCode").get(0).text();
		String returnMessage = doc2.select("responsDDSC").get(0).select("returnMessage").isEmpty()?"":doc2.select("responsDDSC").get(0).select("returnMessage").get(0).text();
		response.setReturnCode(returnCode);
		response.setReturnMessage(returnMessage);
		if(Objects.equals(returnCode,"5000")){
			String swno = doc2.select("responsDDSC").get(0).select("swno").isEmpty()?"":doc2.select("responsDDSC").get(0).select("swno").get(0).text();
			String sellerTaxNo = doc2.select("responsDDSC").get(0).select("sellerTaxNo").isEmpty()?"":doc2.select("responsDDSC").get(0).select("sellerTaxNo").get(0).text();
			response.setSwno(swno);
			response.setSellerTaxNo(sellerTaxNo);
		}
		return response;
	}
	//------------------------------------------------------------------------------发送邮件---分割线---------------------------------------------------------------------------------
	public static MoSendMail getSendMail(String Json,String path){
		MoSendMail sendMail = new MoSendMail();
		JSONObject JSR = JSONObject.parseObject(Json);
		sendMail.setTitle(JSR.getString("title"));
		sendMail.setContext(JSR.getString("context"));
		sendMail.setContainFJ(JSR.getBoolean("isContainFJ"));
		if(sendMail.isContainFJ()){
			sendMail.setFileName(JSR.getString("fileName"));
//			sendMail.setFilePath(path+"\\"+sendMail.getFileName());
			sendMail.setFilePath(path+"/"+sendMail.getFileName());
		}
		sendMail.setToAddress(JSR.getString("toAddress"));
		sendMail.setSend(false);
		sendMail.setInvoiceId(JSR.getString("invoiceId"));
		return sendMail;
	}
	
	
	

}
